﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;
using System.ComponentModel.DataAnnotations;
using System.Data.Entity.ModelConfiguration.Conventions;
using System.Data.Entity.Infrastructure;

namespace EFDemo.Models
{
    public class EFDemoContext : DbContext
    {
        private readonly static string CONNECTION_STRING = "name=WlfSys_EFCF_ConnString";

        public DbSet<Student> Students { get; set; }
        public DbSet<Course> Courses { get; set; }
        public DbSet<Enrollment> Enrollments { get; set; }

        public EFDemoContext()
            : base(CONNECTION_STRING)//不写这个，默认是EFDemoContext
        {

            Database.SetInitializer<EFDemoContext>(new DropCreateDatabaseIfModelChanges<EFDemoContext>());
            
        }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);
            modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();//移除复数表名的契约
            modelBuilder.Conventions.Remove<IncludeMetadataConvention>();//防止黑幕交易 要不然每次都要访问 EdmMetadata这个表

            //base.OnModelCreating(modelBuilder);
            //modelBuilder.Entity<Order>().ToTable("efdemo.Order");
            //modelBuilder.Entity<Order>().Property(x => x.OrderID).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
            //modelBuilder.Entity<Order>().Property(x => x.OrderTitle).IsRequired().HasMaxLength(50);
            //modelBuilder.Entity<Order>().Property(x => x.CustomerName).IsRequired().HasMaxLength(32);
            //modelBuilder.Entity<Order>().Property(x => x.TransactionDate).IsRequired();

        }
    }
}